package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要删除的图书");
        String name = scanner.nextLine();
        int currentSize = bookList.getUseSize();
        int temp = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                temp = i;
                break;
            }
        }
        if(temp == -1){
            System.out.println("没有找到你所要删除的图书！");
            return;
        }
        //开始删除
        for (int j = temp; j <currentSize-1 ; j++) {
            /*bookList.getBooks()[j] = bookList.getBooks()[j+1];*/
            Book book = bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        System.out.println("删除图书成功！");
        bookList.setBook(currentSize-1,null);
        bookList.setUseSize(currentSize-1);
    }
}
